package com.cool.service.sysmanage.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.cool.bo.sysmanage.common.PageQueryBo;
import com.cool.bo.sysmanage.resource.PermissionQueryBo;
import com.cool.mapper.sysmanagemapper.PermissionPoMapper;
import com.cool.po.sysmanagepo.PermissionPo;
import com.cool.service.sysmanage.PermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 资源接口实现类
 *
 * @author Jiangmanman
 * @date 2020/07/29
 */
@Service
public class PermissionServiceImpl implements PermissionService {
    @Resource
    PermissionPoMapper permissionPoMapper;

    @Override
    public PageQueryBo pagingQuery(PermissionQueryBo permissionQueryBo) {

        PageHelper.startPage(permissionQueryBo.getPageNum(), permissionQueryBo.getPageSize());

        List<PermissionPo> list = permissionPoMapper.selectAll(permissionQueryBo);

        PageInfo<PermissionPo> staffPoPageInfo = new PageInfo<>(list);

        PageQueryBo pageQueryBo = new PageQueryBo(
                staffPoPageInfo.getPageSize(),
                staffPoPageInfo.getPageNum(),
                staffPoPageInfo.getPages(),
                staffPoPageInfo.getTotal(),
                staffPoPageInfo.getList()
        );
//        System.out.println(staffPoPageInfo.getList());
        return pageQueryBo;
    }

    @Override
    public Integer deleteById(Integer id) {
        return permissionPoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer updateById(Integer id, PermissionPo permissionPo) {
        permissionPo.setPermissionUpdate(DateUtil.date());
        return permissionPoMapper.updateById(id,permissionPo);
    }

    @Override
    public List<PermissionPo> pagingQueryAll() {




        return permissionPoMapper.selectAllMsg();
    }

    /**
     * 根据URI找到需要的角色
     *
     * @param requestUrl 请求url
     * @return {@link List<String> }
     * @author Jiangmanman
     * @date 2020/07/31
     */
    @Override
    public List<String> selectByUri(String requestUrl) {

        return permissionPoMapper.selectByUri(requestUrl);
    }

    @Override
    public List<String> selectAllUri() {
        return permissionPoMapper.selectAllUri();
    }

    @Override
    public Integer insert(PermissionPo permissionPo) {

        DateTime date = DateUtil.date();

        permissionPo.setPermissionCreate(date);
        permissionPo.setPermissionUpdate(date);

        if(Objects.isNull(permissionPo.getClassifyId())){
            return -1;
        }

        return permissionPoMapper.insertSelective(permissionPo);
    }
}
